﻿#region MIT
/*
Engine Of Evermore (http://code.google.com/p/engineofevermore/)
Copyright (c) 2008, 2009 Michael Woerister

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using EngineOfEvermore.Editor.SceneEditor;
using EngineOfEvermore.Scene;
using EngineOfEvermore.Scene.Visual;
using EngineOfEvermore.Common;
using EngineOfEvermore.Imaging;
using System.Diagnostics;

namespace EngineOfEvermore.Editor.SceneTools
{
    [Mono.Addins.Extension( "/SceneEditor/SceneTools" )]
    public class SelectionSceneTool : EngineOfEvermore.Editor.SceneEditor.ISceneTool
    {
        class ModelContext
        {
            public ModelContext( SceneModel model, Camera camera )
            {
                this.SelectionRectManager = new SelectionRectManager( model.SceneGraph, camera );
                this.SceneGrabber = new SceneGrabber( model.SceneGraph, camera );
                this.ObjectMover = new ObjectMover( model.CommandStack, camera );

                var commandAdapter = new SelectionCommandAdapter( model.Selection,
                    model.CommandStack );
                
                this.SelectionHandler = new SelectionHandler( commandAdapter );
                this.MouseInputHandler = new MouseInputHandler( this.SelectionHandler,
                    this.SceneGrabber, this.SelectionRectManager, this.ObjectMover );
            }

            public SelectionRectManager SelectionRectManager { get; private set; }
            public SceneGrabber SceneGrabber  { get; private set; }
            public MouseInputHandler MouseInputHandler { get; private set; }
            public SelectionHandler SelectionHandler { get; private set; }
            public ObjectMover ObjectMover { get; private set; }
        }

        App _app;
        Dictionary<SceneModel, ModelContext> _modelContexts;

        public SelectionSceneTool()
        {
            _modelContexts = new Dictionary<SceneModel, ModelContext>();
        }

        private ModelContext _getModelContext( SceneModel model )
        {
            ModelContext context;
            if ( _modelContexts.TryGetValue( model, out context ) )
            {
                return context;
            }
            else
            {
                var camera = _app.DocumentManager.OpenDocuments
                    .Single( doc => doc.Model == model ).View.Camera;

                context = new ModelContext( model, camera );

                _modelContexts.Add( model, context );
                return context;
            }
        }

        public bool HandleInputEvent( MouseEventArgs e, InputType type, SceneModel activeModel )
        {
            if ( e.Button == MouseButtons.Left )
            {
                _getModelContext( activeModel ).MouseInputHandler.HandleInputEvent( e, type );
                return true;
            }

            return false;
        }

        public bool HandleInputEvent( KeyEventArgs e, InputType type, SceneModel activeModel )
        {
            switch ( e.KeyCode )
            {
                case Keys.Control:
                case Keys.ControlKey:
                case Keys.RControlKey:
                    _getModelContext( activeModel ).MouseInputHandler.CtrlKeyIsPressed =
                        type == InputType.KeyDown;
                    return true;

                case Keys.Shift:
                case Keys.ShiftKey:
                case Keys.RShiftKey:
                    _getModelContext( activeModel ).MouseInputHandler.ShiftKeyIsPressed =
                        type == InputType.KeyDown;
                    return true;

                default:
                    return false;
            }
        }

        public void Activate()
        {
        }

        public void Deactivate()
        {
        }

        public System.Drawing.Image ButtonImage
        {
            get 
            {
                return Properties.Resources.MouseToolButtonImage;
            }
        }

        public string FriendlyName
        {
            get { return "Selection Tool"; }
        }

        public string ToolTipString
        {
            get { return "Selection Tool - Used for selecting and moving scene objects."; }
        }

        public void Initialize( App app )
        {
            _app = app;
        }
    }
}
